package com.tools.photo.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

import com.tools.photo.R;
import com.tools.photo.bean.PhotoUpImageBucket;
import com.tools.photo.bean.PhotoUpImageItem;
import com.tools.photo.common.DisplayUtils;
import com.tools.photo.common.PhotoUpAlbumHelper;
import com.tools.photo.ui.adapter.MediaAdaptre;
import com.tools.photo.ui.view.BottomSheetDialogView;
import com.tools.photo.ui.widget.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/2/29.
 */
public class MultiSelectorActivity extends AppCompatActivity {

    Button btn;
    RecyclerView mRecycleView;
    List<PhotoUpImageItem> mItems;
    MediaAdaptre adaptre;

    int selectIndex=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selector_layout);
        initData();
        initViews();
        initValue();
    }

    public void setToolbar(){
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        getSupportActionBar().setTitle("图片");
    }

    public void initData(){
        mItems=new ArrayList<>();
        adaptre=new MediaAdaptre(this,mItems);
        setToolbar();
    }

    public void initViews(){
        mRecycleView=(RecyclerView)findViewById(R.id.recycleview);
        btn=(Button)findViewById(R.id.btn);
        mRecycleView.setLayoutManager(new GridLayoutManager(this, 3));
        mRecycleView.setAdapter(adaptre);
    }

    public void initValue(){
        PhotoUpAlbumHelper.getHelper().init(this);
        PhotoUpAlbumHelper.getHelper().setGetAlbumList(new PhotoUpAlbumHelper.GetAlbumList() {
            @Override
            public void getAlbumList(final List<PhotoUpImageBucket> list) {
                mItems.clear();
                mItems.addAll(PhotoUpAlbumHelper.getHelper().getAlbumList());
                adaptre.notifyDataSetChanged();
                btn.setVisibility(View.VISIBLE);
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BottomSheetDialogView bottomSheetDialogView=  new BottomSheetDialogView(v.getContext(), AppCompatDelegate.MODE_NIGHT_AUTO,list,mItems.size(),PhotoUpAlbumHelper.getHelper().getAlbumList().get(0),selectIndex);
                        bottomSheetDialogView.setOnItemsClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mItems.clear();
                                selectIndex=v.getId();
                                if (selectIndex==0){
                                    //所有图片
                                    mItems.addAll(PhotoUpAlbumHelper.getHelper().getAlbumList());
                                }else {
                                    mItems.addAll(list.get(v.getId()-1).getImageList());
                                }
                                adaptre.notifyDataSetChanged();
                            }
                        });
                    }
                });
            }
        });
        PhotoUpAlbumHelper.getHelper().execute(false);
        btn.setVisibility(View.GONE);
    }

}
